package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.entity.User;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        List<Orders> ordersList=orderMapper.getByTime(5,begin,end);

        StringBuilder dateList=new StringBuilder();//2022-10-01,2022-10-02,2022-10-03
        StringBuilder turnoverList=new StringBuilder();//406.0,1520.0,75.0

        int index=0;
        while (!begin.equals(end)){//只要日期还没到end就逐天遍历
            dateList.append(begin).append(',');//将日期凭借拼接
            int count=0;
            while (true){//开始遍历日期下的所有订单
                if (index>=ordersList.size()){
                    break;
                }
                Orders orders = ordersList.get(index);//逐个取出日期
                LocalDate dataTime=orders.getOrderTime().toLocalDate();
                if (!begin.equals(dataTime)){//当日期对不上时退出
                    break;
                }
                count+=orders.getAmount().intValue();
                index++;
            }
            turnoverList.append(count).append(',');
            begin=begin.plusDays(1);
        }
        TurnoverReportVO  result=new TurnoverReportVO();
        dateList.delete(dateList.length()-1,dateList.length());
        turnoverList.delete(turnoverList.length()-1,turnoverList.length());
        result.setDateList(dateList.toString());
        result.setTurnoverList(turnoverList.toString());
        return   result;
    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        StringBuilder dateListSB=new StringBuilder();//日期，以逗号分隔，例如：2022-10-01,2022-10-02,2022-10-03
        StringBuilder totalUserListSB=new StringBuilder();//用户总量，以逗号分隔，例如：200,210,220
        StringBuilder newUserListSB=new StringBuilder();//新增用户，以逗号分隔，例如：20,21,10

        Integer upData=userMapper.getUserByData(begin.plusDays(-1));
        while (!begin.equals(end)){
            Integer count = userMapper.getUserByData(begin);
            dateListSB.append(begin).append(',');//设置日期
            totalUserListSB.append(count).append(',');//总用户
            newUserListSB.append((count-upData)).append(',');//新增用户=今日总用户-昨日总用户
            //更新指针
            begin=begin.plusDays(1);
            upData=count;
        }
        //去除尾巴
        dateListSB.delete(dateListSB.length()-1,dateListSB.length());
        totalUserListSB.delete(totalUserListSB.length()-1,totalUserListSB.length());
        newUserListSB.delete(newUserListSB.length()-1,newUserListSB.length());
        //封装数据
        UserReportVO   result=new UserReportVO();
        result.setDateList(dateListSB.toString());
        result.setTotalUserList(totalUserListSB.toString());
        result.setNewUserList(newUserListSB.toString());

        return result;
    }

    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        StringBuilder dateListSB=new StringBuilder();
        StringBuilder orderCountListSB=new StringBuilder();
        StringBuilder validOrderCountListSB=new StringBuilder();

        List<Orders> ordersList=orderMapper.getByTime(null,begin,end);



        int countOrder=0;
        int ValidOrderCount=0;
        int index=0;
        while (!begin.equals(end)){//只要日期还没到end就逐天遍历
            dateListSB.append(begin).append(',');//将日期凭借拼接
            int count=0;//全部订单计数
            int validOrder=0;//有效订单
            while (true){//开始遍历日期下的所有订单
                if (index>=ordersList.size()){
                    break;
                }
                Orders orders = ordersList.get(index);//逐个取出日期
                LocalDate dataTime=orders.getOrderTime().toLocalDate();
                if (!begin.equals(dataTime)){//当日期对不上时退出
                    break;
                }
                count++;
                if (orders.getStatus()==5){
                    validOrder++;
                }
                index++;
            }
            orderCountListSB.append(count).append(',');//总订单数
            countOrder+=count;
            validOrderCountListSB.append(validOrder).append(',');
            ValidOrderCount+=validOrder;
            begin=begin.plusDays(1);
        }
        //去尾巴
        dateListSB.delete(dateListSB.length()-1,dateListSB.length());
        orderCountListSB.delete(orderCountListSB.length()-1,orderCountListSB.length());
        validOrderCountListSB.delete(validOrderCountListSB.length()-1,validOrderCountListSB.length());
        //数据装箱
        OrderReportVO   result=new OrderReportVO();
        result.setDateList(dateListSB.toString());
        result.setOrderCountList(orderCountListSB.toString());
        result.setValidOrderCountList(validOrderCountListSB.toString());
        result.setTotalOrderCount(countOrder);
        result.setValidOrderCount(ValidOrderCount);
        double d1=countOrder;
        double d2=ValidOrderCount;
        Double OrderCompletionRate=d2/d1;
        result.setOrderCompletionRate(OrderCompletionRate);
        return result;
    }

    @Override
    public SalesTop10ReportVO top(LocalDate begin, LocalDate end) {
        List<GoodsSalesDTO> goodsSalesDTO=orderDetailMapper.selectTop(begin,end);

       StringBuilder nameListSB=new StringBuilder();//商品名称
       StringBuilder numberList=new StringBuilder();//销量

        for (GoodsSalesDTO salesDTO : goodsSalesDTO) {
            nameListSB.append(salesDTO.getName()).append(",");
            numberList.append(salesDTO.getNumber()).append(",");
        }
        nameListSB.delete(nameListSB.length()-1,nameListSB.length());
        numberList.delete(numberList.length()-1,numberList.length());
        SalesTop10ReportVO result=new SalesTop10ReportVO();
        result.setNameList(nameListSB.toString());
        result.setNumberList(numberList.toString());
        return result;
    }
}
